﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastORM.Applicaiton;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("Group")]
    public class GrouoController : FastTemplateController<Frame_Group>
    {
        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/Group/Group_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/Group/Group_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/Group/Group_Edit.cshtml");
        }

        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string GroupName = Convert.ToString(Params["GroupName"]);
            string ParentGroupCode = Convert.ToString(Params["ParentGroupCode"]);
            bool SearchInAll = Convert.ToBoolean(Params["SearchInAll"]);
            if (SearchInAll)
            {
                if (!string.IsNullOrEmpty(GroupName))
                {
                    ListWhereText += string.Format(" and GroupName like '%{0}%'", GroupName);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(GroupName))
                {
                    ListWhereText += string.Format(" and GroupName like '%{0}%'", GroupName);
                }
                if (!string.IsNullOrEmpty(ParentGroupCode))
                {
                    if (ParentGroupCode == "Top")
                    {
                        ListWhereText += string.Format(" and LENGTH(GroupCode)={0}", 4);
                    }
                    else
                    {
                        ListWhereText += string.Format(" and LENGTH(GroupCode)={0} and GroupCode like '{1}%'", ParentGroupCode.Length + 4, ParentGroupCode);
                    }

                }
            }
            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_Group Entity)
        {

            return base.OperateAdd(Entity);
        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }

        [Route("Tree")]
        public ActionResult Tree()
        {
            JObject Params = GetParams();
            string GroupTag = Convert.ToString(Params.GetValue("GroupTag"));
            ReturnValue result = new ReturnValue();
            List<Frame_Group> AllOUList = DB_Fast.FindAll("", "OrderNum desc");
            List<Tree> AllTreeList = new List<Tree>();
            foreach (Frame_Group item in AllOUList)
            {
                Tree treenode = new Tree();
                treenode.id = item.RowGuid;
                treenode.label = item.GroupName;
                treenode.paerntGuid = item.ParentGroupGuid;
                treenode.AddAtttibute("groupcode", item.GroupCode);
                AllTreeList.Add(treenode);
            }
            List<Tree> TreeNodeList = null;
            if (!string.IsNullOrEmpty(GroupTag))
            {
                DBRow row = new DBRow("Frame_Group", "GroupTag", GroupTag);
                if (row.IsExist())
                {
                    TreeNodeList = TreeBuilder.Build(Convert.ToString(row["RowGuid"]), AllTreeList);
                }
                else
                {
                    TreeNodeList = TreeBuilder.Build("Top", AllTreeList);
                }
            }
            else
            {
                TreeNodeList = TreeBuilder.Build("Top", AllTreeList);
            }
            result.Add("tree", TreeNodeList);
            return result;
        }
    }
}
